package Tests;

import ru.ssau.view.InvalidDateException;
import ru.ssau.view.DateSetPanel;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class DateSetPanelTest {

    public class TimeStorage {

        private int year;
        private int month;
        private int day;
        private int hours;
        private int minutes;
        private int seconds;

        public TimeStorage(int year, int month, int day, int hours,
                int minutes, int seconds) {
            this.year = year;
            this.month = month;
            this.day = day;
            this.hours = hours;
            this.minutes = minutes;
            this.seconds = seconds;
        }

        public long calculatePredefinedTime() {
            GregorianCalendar c = new GregorianCalendar(year, month - 1, day, hours, minutes, seconds);
            return c.getTimeInMillis();
        }

        public long calculateTestTime() throws InvalidDateException {
            DateSetPanel dsp = new DateSetPanel();
            dsp.setDate(year, month, day, hours, minutes, seconds);
            return dsp.getDate().getTime();
        }
    }

    public DateSetPanelTest() {
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    //18.10.2011: 1 1 59 - correct
    public void test1() {
        TimeStorage ts = new TimeStorage(2011, 10, 18, 1, 1, 59);
        try {
            assertTrue(ts.calculatePredefinedTime() == ts.calculateTestTime());
        } catch (InvalidDateException ex) {
            Logger.getLogger(DateSetPanelTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Test
    //28.02.2011 0 0 1- correct
    public void test2() {
        TimeStorage ts = new TimeStorage(2011, 2, 28, 0, 0, 1);
        boolean cond=false;
        try {
            cond=ts.calculatePredefinedTime() == ts.calculateTestTime();
        } catch (InvalidDateException ex) {
            Logger.getLogger(DateSetPanelTest.class.getName()).log(Level.SEVERE, null, ex);
        }
        assertTrue(cond);
    }

    @Test(expected=InvalidDateException.class)
    //29.02.2011 - incorrect
    public void test3() throws InvalidDateException {
        TimeStorage ts = new TimeStorage(2011, 2, 29, 1, 1, 1);
        ts.calculateTestTime();
    }

    @Test
    //29.02.2012 1 1 1- correct
    public void test4() {
        TimeStorage ts = new TimeStorage(2012, 2, 29, 1, 1, 1);
        boolean cond=false;
        try {
            cond=ts.calculatePredefinedTime() == ts.calculateTestTime();
        } catch (InvalidDateException ex) {
            Logger.getLogger(DateSetPanelTest.class.getName()).log(Level.SEVERE, null, ex);
        }
        assertTrue(cond);
    }

    @Test(expected=InvalidDateException.class)
    //21.13.2011 - incorrect
    public void test5() throws InvalidDateException {
        TimeStorage ts = new TimeStorage(2011, 13, 21, 1, 1, 1);
        ts.calculateTestTime();
    }

    @Test
    //21.01.1800 1 1 1- correct
    public void test6() {
        TimeStorage ts = new TimeStorage(1800, 1, 21, 1, 1, 1);
        boolean cond=false;
        try {
            cond=ts.calculatePredefinedTime() == ts.calculateTestTime();
        } catch (InvalidDateException ex) {
            Logger.getLogger(DateSetPanelTest.class.getName()).log(Level.SEVERE, null, ex);
        }
        assertTrue(cond);
    }

    @Test(expected=InvalidDateException.class)
    //21.01.2011 24 1 1 - incorrect
    public void test7() throws InvalidDateException {
        TimeStorage ts = new TimeStorage(2011, 1, 21, 24, 1, 1);
        ts.calculateTestTime();
    }

    @Test(expected=InvalidDateException.class)
    //21.01.2011 23 60 1 - incorrect
    public void test8() throws InvalidDateException {
        TimeStorage ts = new TimeStorage(2011, 1, 21, 23, 60, 1);
        ts.calculateTestTime();
    }

    @Test(expected=InvalidDateException.class)
    //21.01.2011 23 -1 1 - incorrect
    public void test9() throws InvalidDateException {
        TimeStorage ts = new TimeStorage(2011, 1, 21, 23, -1, 1);
        ts.calculateTestTime();
    }

    @Test(expected=InvalidDateException.class)
    //21.01.2011 24 1 60 - incorrect
    public void test10() throws InvalidDateException {
        TimeStorage ts = new TimeStorage(2011, 1, 21, 23, 1, 60);
        ts.calculateTestTime();
    }
}
